//
//  DECXTabBarViewController.m
//  DECXInStore
//
//  Created by 程强 on 15/12/9.
//  Copyright © 2015年 程强. All rights reserved.
//

#import "DECXTabBarViewController.h"
#import "SYTabBar.h"
#import "SYTabBarItem.h"
@interface DECXTabBarViewController ()<SYTabBarDelegate>
@property (strong, nonatomic) SYTabBar *bar;
@property (strong, nonatomic) NSMutableArray *itemAry;
@end

@implementation DECXTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *titleAry = @[@"首页",@"我的"];
    NSArray *imgAry = @[LoadImage(@"home_s"),LoadImage(@"setting_s")];
    NSArray *selectImgAry = @[LoadImage(@"home"),LoadImage(@"setting")];
    
    self.itemAry = [NSMutableArray new];
    
    for (int i = 0; i<titleAry.count; i++) {
        SYTabBarItem *item = [[SYTabBarItem alloc]initWithFrame:CGRectMake(0+i*ScreenWidth/2, 0, ScreenWidth/2, 44) title:titleAry[i] selectedImage:imgAry[i] unselectedImage:selectImgAry[i]];
        item.unselectedTitleColor = [UIColor whiteColor];
        item.selectedTitleColor = [UIColor whiteColor];
        if (i == 0) {
            [item setSelected:YES];
        }
        item.syTag = i;
        [_itemAry addObject:item];
    }
    self.bar = [[SYTabBar alloc]initWithSYTabBarItems:_itemAry];
    [_bar setShowDividingLine:NO];
    _bar.tabBarDelegate = self;
    [self.tabBar insertSubview:_bar atIndex:0];
    
    UIViewController *vc1 = StoryBoardViewController(@"Main", @"HomeNavi");
    UIViewController *vc2 = StoryBoardViewController(@"Main", @"SettingNavi");
    
    self.viewControllers = @[vc1,vc2];
    
    for (UIView *view in [self.tabBar subviews]) {
        NSString *str = NSStringFromClass([view class]);
        if ([str isEqualToString:@"UITabBarButton"]) {
            view.userInteractionEnabled = NO;
        }
    }

    
}
- (void)tabBar:(SYTabBar *)tabBar didSelectItem:(SYTabBarItem *)item
{
    [self setSelectedIndex:item.syTag];
    UINavigationController *naviVC = self.viewControllers[item.syTag];
    [naviVC.topViewController.navigationController popToRootViewControllerAnimated:NO];
}
- (void)setSelectItem:(NSInteger)index
{
    [self.bar setSelectedItem:_itemAry[index]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
